/**登录**/
var localStorageManagers;
var localStoragerManager = require('script/localStorageManagers.js');
var storageUtils = require('script/localStorageUtils.js');
(function($, doc) {
	$.init({
	       beforeback: function(){
		     var userid=localStorageManagers.getUseridSession();
		     if(userid==null){
		     	plus.runtime.quit();
		     	return false;
		     }
		     return true;
	       }
});
	var loginWebview;
	localStorageManagers=require("script/localStorageManagers.js");
	$.plusReady(function() {
		//isFirstLogin();
		setUserNameAndPassword();
		loginWebview=plus.webview.currentWebview();
		//登录按钮事件  start
		var loginBtn = document.getElementById('login');
		loginBtn.addEventListener('tap', function(event) {
			
			var username = document.getElementById('username').value;
			var password = document.getElementById('password').value;
			if (username.length == 0 || password.length == 0) {
				plus.nativeUI.toast("帐号和密码不能为空");
				return;
			}
			password = hex_md5(password);
			console.log("登录："+serverAjax+'mobile/index/login.do');
			var w = plus.nativeUI.showWaiting();
			mui.ajax(serverAjax+'mobile/index/login.do', {
				data: {
					username: username,
					password: password
				},
				dataType: 'json', //服务器返回json格式数据
				type: 'post', //HTTP请求类型
				timeout: 10000, //超时时间设置为10秒；
				success: function(data) {
						w.close();
					if (data.code == 1) {
						//缓存用户Id
						localStorageManagers.setUsernameSession(username);
						localStorageManagers.setUserpasswordSession(document.getElementById('password').value);
						
						localStorageManagers.setUseridSession(data.user.userID);
						localStorageManagers.setUserinfoSession(data.user.ext1);
						
						mui.openWindow({
			url: 'index.html',
			id: 'index',
			show: {
				aniShow: 'pop-in'
			},
			styles: {
				popGesture: 'hide'
			},
			waiting: {
				autoShow: false
			}
		});
						//刷新课程页面内容
						var courseWebView = plus.webview.getWebviewById("synccourse/courseindex.html");
						if(courseWebView!=null)courseWebView.evalJS("loadCourse()");
						//刷新设置页面信息
						var setupWebView = plus.webview.getWebviewById("setup/setupindex");
						if(setupWebView!=null)setupWebView.evalJS("initSetupinfo()");
						//刷新头像
						var selfWebView = plus.webview.getWebviewById("selfcenter/selfindex.html");
						if(selfWebView!=null)selfWebView.evalJS("setUsertouxiang()");
						
						//mui.back();
						var indexWebview = plus.webview.getWebviewById("index.html");
						//if(data.user.mobileTelNumber==''){
						//	mui.openWindow({
						//		url:'setup/bindmobile.html',
						//		id:'setup/bindmobile'
						//	});
						//}
						//w.close();
					} else {
						w.close();
						mui.toast(data.info);
					}
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					ajaxErrorTip();
					w.close();
					console.log(type);
				}
			});
		
		});
		//登录按钮事件  end
		//忘记密码   start
		var forgetButton = doc.getElementById('forgetPassword');
		forgetButton.addEventListener('tap', function(event) {
				$.openWindow({
					url: 'setup/forgetpassword.html',
					id: 'setup/forgetpassword',
					show: {
						aniShow: 'pop-in'
					},
					styles: {
						popGesture: 'hide'
					},
					waiting: {
						autoShow: false
					}
				});
			}, false);
		//忘记密码   end
	});
}(mui, document));

//记住用户名和密码
function setUserNameAndPassword(){
	var username=localStoragerManager.getUsernameSession();
	var password=localStoragerManager.getUserpasswordSession();
	if (username != null && username.length>0) {
	 document.getElementById('username').value=username;
	 document.getElementById('password').value=password;
	}
}

//是否初次进入
function isFirstLogin() {
	var isfirst = storageUtils.getSessionItem("isfirst");
	var result = false;
	if (isfirst == null || isfirst.length == 0) {
		mui.openWindow({
			url: 'startpage.html',
			id: 'startpage',
	     	waiting:{
               autoShow:false//自动显示等待框，默认为true
            }
		});
		storageUtils.setSessionItem('isfirst', 'true');
		result = true;
	}
	//storageUtils.removeSessionItem('isfirst');
	return result;
};